import {WeaFormItem,WeaLocaleProvider, WeaSearchGroup} from 'ecCom';
import {WeaSwitch} from 'comsMobx';
import {message} from 'antd';
const getLabel = WeaLocaleProvider.getLabel;
import isFunction from 'lodash/isFunction';

const DocDetailConstant = {
  createFormItem: (item, form, otherParams) => {
    const formParams = form.getFormParams();
    let fieldConfg = {...item};
    if (otherParams && otherParams.viewAttr) {
      fieldConfg.viewAttr = otherParams.viewAttr;
    }
    let fieldsOnChange = fieldConfg.onChange;
    delete fieldConfg.onChange;
    return item ? (
      <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@m5ox2m`}
        label={`${item.label}`}
        labelCol={{ span: `${item.labelcol || 8}` }}
        wrapperCol={{ span: `${item.fieldcol || 14}` }}
        error={form.getError(item)}
        style={item.style || {}}
        underline={item.underline}
        tipPosition="bottom">
        {item.conditionType != 'CUSTOM' &&
          <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@mwslae`} fieldConfig={fieldConfg} form={form} formParams={formParams}
                     onChange={(...args) => {
                       isFunction(fieldsOnChange) && fieldsOnChange(...args);
                     }}
          />}
        {item.otherCom}
      </WeaFormItem>
    ) : null;
  },
  createFormItems: (items, form) => {
    if (!Array.isArray(items)) {
      return [];
    }
    return items.map((item) => DocDetailConstant.createFormItem(item, form)).filter((com) => !!com);
  },
  getSearchs: (conditions,form, otherParams= {col:1}) => {
    if (!Array.isArray(conditions)) {
      return [];
    }
    let group = [];
    conditions.map((c, index) => {
      let items = c.items.map((item) => {
        return {
          com: DocDetailConstant.createFormItem(item, form, otherParams),
        };
      }).filter((com) => !!com.com);
      let searchGroup = (
        <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@bf93l3@${index}`} showGroup={c.defaultshow} {...c} col={otherParams.col} items={items} />
      );
      group.push(searchGroup)
    });
    return group;
  },
}

export default DocDetailConstant;